package Sort;

import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {1,10,7,4,8,3,2,6,9,5};
        select2(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void select1(int[] arr) {

        for (int i = 0;i < arr.length - 1;i++) {
            int minIndex = i;
            for (int j = i + 1;j < arr.length;j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            if (i != minIndex) {
                swap(arr,i,minIndex);
            }
        }
    }

    public static void swap(int[] arr,int i,int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void select2(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;

            for (int i = left+1;i <= right; i++) {
                if (arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
                if (arr[i] > arr[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(arr,minIndex,left);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(arr,maxIndex,right);
            left++;
            right--;
        }
    }

}
